package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.activity.result.*;

public class MainActivity2 extends AppCompatActivity {

    Button button;

    TextView textView;



    ActivityResultLauncher<Intent> activityResultLauncher;

    public MainActivity2() {
        super();
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d("zyy","2:onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("zyy","2:onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("zyy","2:onDestroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("zyy","2:onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("zyy","2:onRestart");
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        Log.d("zyy","2:onCreate"); //logcat调试用

        button=findViewById(R.id.button2);

        textView=findViewById(R.id.textView4);

        Intent intent = new Intent(this,MainActivity21.class);

//        intent.putExtra("name","zyy");
//
//        intent.putExtra("age",20);

        Bundle bundle=new Bundle();

        bundle.putString("name","zyy");

        bundle.putInt("age",20);

        intent.putExtras(bundle);

        activityResultLauncher=registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if(result.getResultCode()== 666){
                        assert result.getData() != null;
                        String str = result.getData().getStringExtra("result");
                        textView.setText(str);
                    }
                }
        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startActivity(intent);
                activityResultLauncher.launch(intent);  //强化版startActivity
            }
        });
    }
}

